import util from "../../utils/util"
import Api from "../../utils/api"
Page({
    data: {
        addrInfo: [],
        userinfo: {
            id: 0,
            areaCode: 0,
            address: '',
            name: '',
            tel: '',
            isDefault: 0,
            province: '',
            city: '',
            county: '',
            address_detail:''
        },
        checkStatus: true,
        status: "default" //状态分add,modify
    },

    onLoad: function (option) {
        var status = option.status;
        console.log(status);
        this.setData({
            status: option.status
        })
        //如果为修改状态可以进行值赋值
        if (status == 'modify') {
            var userinfo = JSON.parse(option.userinfo);
            this.setData({
                userinfo: userinfo,
            })
        }
    },
    // 选择微信自带地址
    addAddr: function () {
        var that = this;
        //选择地址
        wx.chooseLocation({
            success: function (res) {
                console.info(res);
                var userinfo = that.data.userinfo;
                userinfo.province = res.address;
                userinfo.addressDetail = res.name;
                that.setData({
                    userinfo: userinfo
                })
            },
        })
    },
    //地址是否默认
    onChange({
                 detail
             }) {
        //获取用户信息
        var userinfo = this.data.userinfo;
        userinfo.isDefault = detail;

        // 需要手动对用户的checked默认状态进行更新
        this.setData({
            userinfo: userinfo
        });
    },
    //添加地址
    addAddress: function () {
        let that = this;
        let userinfo = this.data.userinfo;
        if (userinfo.name == '') {
            util.showErrorToast('请输入姓名');
            return false;
        }
        if (userinfo.tel == '') {
            util.showErrorToast('请输入手机号码');
            return false;
        }
        if (userinfo.province == '') {
            util.showErrorToast('请输入省市区');
            return false;
        }
        if (userinfo.addressDetail == '') {
            util.showErrorToast('请输入详细地址');
            return false;
        }
        util.request(Api.wx.AddressSave, {
            id: userinfo.id?userinfo.id:0,
            name: userinfo.name,
            tel: userinfo.tel,
            province: userinfo.province,
            city: userinfo.city,
            county: userinfo.county,
            areaCode: userinfo.areaCode,
            addressDetail: userinfo.addressDetail,
            isDefault: userinfo.isDefault
        }, 'POST').then(function (res) {
            if (res.errno === 0) {
                //返回之前，先取出上一页对象，并设置addressId
                var pages = getCurrentPages();
                var prevPage = pages[pages.length - 2];
                console.log(prevPage);
                if (prevPage.route == "pages/payinfo/payinfo") {
                    prevPage.setData({
                        userinfo: res.data
                    })
                    try {
                        wx.setStorageSync('userinfo', res.data);
                    } catch (e) {

                    }
                    console.log("set address");
                }
                wx.navigateBack();
            }
        });
        // var currentPage = getCurrentPages();
        // var prevPage = currentPage[currentPage.length - 2]; //上一个页面

        // var userinfo = this.data.userinfo;

        // prevPage.setData({
        //     status: 'add',
        //     userinfo: userinfo
        // })
        // wx.navigateBack({
        //     delta: 1
        // })
        // console.log(currentPage);
    },
    //值修改实时赋值
    modifyValue: function (event) {
        var text = event.target.dataset.text; //文本信息
        var value = event.detail; //值
        var userinfo = this.data.userinfo; //获取当前的用户信息
        switch (text) {
            case "name":
                userinfo.name = value;
                break;
            case "tel":
                userinfo.tel = value;
                break;
            case "province":
                userinfo.province = value;
                break;
            case "addressDetail":
                userinfo.addressDetail = value;
                break;
            default:
        }
        this.setData({
            userinfo: userinfo
        })
    },
    //删除地址
    delAddress: function () {
        let userinfo = this.data.userinfo;
        util.request(Api.wx.AddressDel, {
            id: userinfo.id?userinfo.id:0,
            name: userinfo.name,
            tel: userinfo.tel,
            province: userinfo.province,
            city: userinfo.city,
            county: userinfo.county,
            areaCode: userinfo.areaCode,
            addressDetail: userinfo.addressDetail,
            isDefault: userinfo.isDefault
        }, 'POST').then(function (res) {
            if (res.errno === 0) {
                //var currentPage = getCurrentPages();
                //var prevPage = currentPage[currentPage.length - 2]; //上一个页面
                //var userinfo = this.data.userinfo; //获取当前的用户信息
                // prevPage.setData({
                //     status: 'delete',
                //     userinfo: userinfo
                // })
                wx.navigateBack({
                    delta: 1
                })
            }
        });
        
    },
    //修改地址
    modifyAddress: function () {
        var currentPage = getCurrentPages();
        var prevPage = currentPage[currentPage.length - 2]; //上一个页面
        var userinfo = this.data.userinfo; //获取当前的用户信息
        prevPage.setData({
            status: 'modify',
            userinfo: userinfo
        })
        wx.navigateBack({
            delta: 1
        })
        console.log(currentPage);
    }

})